package com.sacc.util;

import com.power.common.model.CommonResult;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

@Component
public class OssUtil {
    private static final String ACCESSKEY = "AKIDKSKvGK7dsqZsvlLfhSwA7oEOHsCF2Zpn";
    private static final String SECRETKEY = "jBMGuHMAhyyiBOKzNUKtBCwPbWBZgW6t";
    private static final String REGION = "ap-nanjing";
    private static final String BUCKETNAME = "fengyuan-1307699548";
    private static final String APPID = "1307699548";
    private static final String PATH = "https://fengyuan-1307699548.cos.ap-nanjing.myqcloud.com";
    private static final String QIANZHUI = "fengyuan";

    public static CommonResult Upload(MultipartFile file){
        if(file == null){
            return new CommonResult(false,"文件为空",null);
        }
        String oldFileName = file.getOriginalFilename();
        String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID()+eName;
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month=cal.get(Calendar.MONTH);
        int day=cal.get(Calendar.DATE);
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(ACCESSKEY, SECRETKEY);
        // 2 设置bucket的区域
        ClientConfig clientConfig = new ClientConfig(new Region(REGION));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
        String bucketName = BUCKETNAME;
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
        // 大文件上传请参照 API 文档高级 API 上传
        File localFile = null;
        try {
            localFile = File.createTempFile("temp",null);
            file.transferTo(localFile);
            // 指定要上传到 COS 上的路径
            String key = "/"+QIANZHUI+"/"+year+"/"+month+"/"+day+"/"+newFileName;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            return new CommonResult(true,"上传成功",PATH + putObjectRequest.getKey());
        } catch (IOException e) {
            return new CommonResult(false,e.getMessage(),null);
        }finally {
            // 关闭客户端(关闭后台线程)
            cosclient.shutdown();
        }

    }
    public static CommonResult Delete(String url){
        String key = url.replaceAll(PATH + '/', "");
        System.out.println(key);
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(ACCESSKEY, SECRETKEY);
        // 2 设置bucket的区域
        ClientConfig clientConfig = new ClientConfig(new Region(REGION));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        try {
            cosclient.deleteObject(BUCKETNAME,key);
            return new CommonResult(true,"删除成功",url);
        } finally {
            // 关闭客户端(关闭后台线程)
            cosclient.shutdown();
        }
    }
}
